package com.navinfo.collect.library.map.flutter.plugin

import android.content.Context
import com.navinfo.collect.library.data.flutter.FlutterDataController
import com.navinfo.collect.library.map.NIMapOptions
import com.navinfo.collect.library.map.flutter.FlutterMapConversion
import com.navinfo.collect.library.map.flutter.FlutterMapController
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.StandardMessageCodec
import io.flutter.plugin.platform.PlatformView
import io.flutter.plugin.platform.PlatformViewFactory

class FlutterMapViewFactory(
    private val messenger: BinaryMessenger,
    lifecycleProxy: LifecycleProxy
) :
    PlatformViewFactory(StandardMessageCodec.INSTANCE) {
    private val binaryMessenger: BinaryMessenger = messenger

    private var mLifecycleProxy: LifecycleProxy = lifecycleProxy

    override fun create(context: Context?, id: Int, args: Any?): PlatformView {
        val mapController: FlutterMapController = buildMapController(context!!, id, args!!)
        val dataController = buildDataController(context, id)
        return FlutterMapView(mapController, dataController, mLifecycleProxy)
    }

    /**
     * 创建地图控制器
     */
    private fun buildMapController(context: Context, viewId: Int, args: Any): FlutterMapController {

        val options: NIMapOptions = FlutterMapConversion.flutterToNIMapOptions(args.toString())
        return FlutterMapController(viewId, context, binaryMessenger, options)
    }

    /**
     * 创建数据控制器
     */
    private fun buildDataController(context: Context, viewId: Int): FlutterDataController {
        return FlutterDataController(viewId, context, binaryMessenger)
    }

}
